package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 导出 生财有道1-7服 每个区首轮【赚速+冲榜】榜单结算后，前一百名玩家数据。
 * @Author xx
 * @Date 2020/11/11
 */
public class GetFirstES extends BaseRepair {

    public GetFirstES(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(int activityId){
        if(activityId != 0){
            List<Map<String,String>> userData = getData(activityId);
            DataMgr.addData(userData);
        }
    }

    /*private int getActivity() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_activity_main WHERE serverId = ? AND type = 1 AND childType = 2 ORDER BY beginShowTime ASC LIMIT 1 ";
        try {
            ServerMgr.getServerDb().setConnName(DbNameStrategy.getServerDb());
            call = ServerMgr.getServerDb().getConnection().prepareCall(sql);
            call.setLong(1, getGameServer().getServerInfo().getServerId());
            rs = call.executeQuery();
            while (rs.next()) {
                return rs.getInt("activityId");
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            ServerMgr.getServerDb().closeConnection(call, null);
        }
        return 0;
    }*/

    private List<Map<String,String>> getData(int activityId) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select u.userId,FROM_UNIXTIME(u.registerTime,'%Y年%m月%d %H:%i:%s') as registerTime from t_u_activity_condition ac" +
                " LEFT JOIN t_u_user u on ac.userId = u.userId" +
                " WHERE ac.activityId = ? ORDER BY ac.`value`+0 DESC LIMIT 100";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            int rank = 0;
            while (rs.next()){
                Map<String,String> temp = new ConcurrentHashMap<>();
                String userId = rs.getString("userId");
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",userId);
                temp.put("rank",String.valueOf(++rank));
                temp.put("registerTime",checkNullToEmpty(rs.getString("registerTime")));
                List<Map<String,String>> orderList = getUserRechargeOrder(userId);
                for(Map<String,String> orderMap : orderList){
                    orderMap.put("serverId",temp.get("serverId"));
                    orderMap.put("userId",temp.get("userId"));
                    orderMap.put("rank",temp.get("rank"));
                    orderMap.put("registerTime",temp.get("registerTime"));
                    dataList.add(orderMap);
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
            return dataList;
        }
    }

    private List<Map<String,String>> getUserRechargeOrder(String userId){
        List<Map<String,String>> data = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select mallId,amount from t_u_recharge_detail WHERE userId = ? AND (orderStatus = 1 OR orderStatus = 2)";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            call.setString(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("mallId",rs.getString("mallId"));
                temp.put("amount",rs.getString("amount"));
                data.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
            return data;
        }
    }
}
